package com.vtb.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 消息内容处理
 *
 * @author Chen Tao
 * @date 2012-7-2 下午4:07:04
 */
public class MessageUtils {
	private static final String[] reString = { "(调皮", "(尴尬","(微笑",
			"(惊讶", "(哭", "(惊恐", "(困", "(可爱",
			"(偷笑", "(酷", "(难过", "(晕", "(吻",
			"(吃饭", "(谄笑" };
	private static final String[] repString = { "/调皮", "/尴尬","/微笑", "/惊讶",
			"/大哭", "/惊恐", "/困", "/可爱", "/偷笑", "/酷", "/难过",
			"/晕", "/献吻", "/饭", "/坏笑" };
	private static final String[] qq = {"/色","/睡","/发怒","/吐","/嘘...","/衰","/骷髅",
		"/敲打","/再见","/糗大了","/左哼哼","/右哼哼","/快哭了","/吓","/刀","/强","/弱",
		"/NO","/OK","/左太极","/右太极"};
	private static final String ps1 = "\\(\\S{1,5}\\)"; 
	private static final String ps2 = "\\/\\S{1,4}";
	private static final String ps3 = "\\/\\S{1,3}";
	private static final String ps4 = "\\/\\S{1,2}";
	private static final String psurl = "http://[\\S\\.]+[:\\d]?[/\\S]+\\??[\\S=\\S&?]+[^\u4e00-\u9fa5]";
	private static String temp;
	private static String replaceString = "vtburltag";
	/**
	 * 转换rr表情到qq
	 *
	 * @param message
	 * @return
	 * @author Chen Tao
	 * @date 2012-7-2 下午4:06:04
	 */
	@SuppressWarnings("static-access")
	public static String rr2qqExpression(String message) {
		Pattern pattern = Pattern.compile(ps1);
		Matcher matcher = pattern.matcher(message);
		if (matcher.find()) {
			for (int i = 0; i < reString.length; i++) {
				matcher = pattern.compile("\\"+reString[i]+"\\)").matcher(message);
				if (matcher.find()) {
					message = matcher.replaceAll("\\"+repString[i]);
				}
			}
			message = Pattern.compile(ps1).matcher(message).replaceAll("");
		}
		return message;
	}
	/**
	 * 转换qq表情到rr
	 *
	 * @param message
	 * @return
	 * @author Chen Tao
	 * @date 2012-7-2 下午4:06:28
	 */
	@SuppressWarnings("static-access")
	public static String qq2rrExpression(String message) {
		message = torrUrl(message);
		Pattern pattern = Pattern.compile(ps2);
		Matcher matcher = pattern.matcher(message);
		if (matcher.find()) {
			for (int i = 0; i < reString.length; i++) {
				matcher = pattern.compile("\\"+repString[i]).matcher(message);
				if (matcher.find()) {
					message = matcher.replaceAll("\\"+reString[i]+"\\)");
				}
			}
		}
		matcher = Pattern.compile(ps3).matcher(message);
		if (matcher.find()) {
			for (int i = 0; i < qq.length; i++) {
				matcher = pattern.compile("\\"+qq[i]).matcher(message);
				if (matcher.find()) {
					message = matcher.replaceAll("");
				}
			}
			message = Pattern.compile(ps4).matcher(message).replaceAll("");
		}
		if (temp != null) {
			message = message.replace(replaceString, temp);
		}
		return message;
	}
	
	/**
	 * 消息中是否包含某字符串
     * @param message  消息源
     * @param contain  被包含字符串
	 * @return
	 * @author Chen Tao
	 * @date 2012-7-2 下午4:06:41
	 */
	public static boolean cotainString(String message , String contains){
		String[] contain = contains.split(",");
		for (int i = 0; i < contain.length; i++) {
			if (message.lastIndexOf(contain[i]) >= 0) {
				return true;
			}
		}
		return false;
	} 
	/**
	 * 消息内容以某关键字符结尾
	 *
	 * @param message
	 * @param key
	 * @return
	 * @author Chen Tao
	 * @date 2012-7-2 下午4:09:54
	 */
	public static boolean lastIndex(String message , String key){
	    if (message.lastIndexOf(key)==message.length()-1) {
            return true;
        }
	    return false;
	}
	
	/**
	 *  提取url
	 *
	 * @param message
	 * @return
	 * @author Chen Tao
	 * @date 2012-7-2 下午4:06:55
	 */
	public static String torrUrl(String message){
		temp = null;
		Pattern pattern = Pattern.compile(psurl);
		Matcher matcher = pattern.matcher(message);
		if (matcher.find()) {
			temp = matcher.group();
			message = matcher.replaceAll(replaceString);
		}
		return message;
	}
	
}
